<?php
declare (strict_types = 1);
namespace app\admin\controller;

use think\Request;
use think\facade\{View, Session};
use think\exception\ValidateException;
use service\JsonService as Json;

use app\common\model\{Shop, ShopCate};
use app\common\validate\{ShopValidate, ShopCateValidate};

class ShopController extends BaseController
{
    /**
     * 渲染店铺列表页面
     */
    public function shop_list()
    {
        return view();
    }

    /**
     * 店铺列表数据
     */
    public function shop_data()
    {
        $data = input('');

        try{
            validate(ShopValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            return Json::fail($e->getError());
        }

        $where = [];

        /** 搜索条件 */
        if($data && isset($data['key'])){
            if(!empty($data['key']['name'])){
                $where[] = ['name', 'like', '%' . $data['key']['name'] . '%'];
            }
        }

        $list = Shop::getList($where, 1, $data['page'], $data['limit']);

        if($list === false) return Json::fail('暂无数据~');
        return Json::success('ok', $list, Shop::getNum($where));
    }

    /**
     * 店铺加盟申请通过
     */
    public function shop_pass()
    {
        $data = input('');

        try{
            validate(ShopValidate::class)->scene('admin_show')->check($data);
        }catch(ValidateException $e){
            return Json::fail($e->getError());
        }

        $data['status'] = 1;
        $flag = Shop::edit($data);

        if($flag === false) return Json::fail('通过失败');
        return Json::success('通过成功');
    }

    /**
     * 店铺加盟申请驳回
     */
    public function shop_back()
    {
        $data = input('');

        try{
            validate(ShopValidate::class)->scene('back')->check($data);
        }catch(ValidateException $e){
            return Json::fail($e->getError());
        }

        $data['status'] = 2;
        $flag = Shop::edit($data);

        if($flag === false) return Json::fail('驳回失败');
        return Json::success('驳回成功');
    }

    /**
     * 渲染店铺编辑页面
     */
    public function shop_edit()
    {
        $data = input('');

        $info = Shop::getFind(['id' => $data['id']]);

        $addressArr = explode('-', $info['address']);
        $info['province'] = $addressArr[0];
        $info['city']     = $addressArr[1];
        $info['area']     = $addressArr[2];
        $info['street']   = $addressArr[3];

        $cate_list = ShopCate::getList([]);

        View::assign('info', $info);
        View::assign('cate_list', $cate_list);
        View::assign('cate_arr', explode(',', (string)$info->getData('cate_id')));
        return view();
    }

    /**
     * 店铺信息修改
     */
    public function shop_edit_data()
    {
        $data = input('');

        try{
            validate(ShopValidate::class)->scene('edit')->check($data);
        }catch(ValidateException $e){
            return Json::fail($e->getError());
        }

        $data['address'] = $data['province'] . '-' . $data['city'] . '-' . $data['area'] . '-' . $data['street'];
        $flag = Shop::edit($data);

        if($flag === false) return Json::fail('修改失败');
        return Json::success('修改成功');
    }










    /**
     * 渲染分类列表页面
     */
    public function cate_list()
    {
        return view();
    }

    /**
     * 分类列表数据
     */
    public function cate_data()
    {
        $data = input('');

        try{
            validate(ShopCateValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            return Json::fail($e->getError());
        }

        $where = [];

        /** 搜索条件 */
        if($data && isset($data['key'])){
            if(!empty($data['key']['name'])){
                $where[] = ['name', 'like', '%' . $data['key']['name'] . '%'];
            }
        }

        $list = ShopCate::getList($where, 1, $data['page'], $data['limit']);

        if($list === false) return Json::fail('暂无数据~');
        return Json::success('ok', $list, ShopCate::getNum($where));
    }

    /**
     * 渲染分类添加页面
     */
    public function cate_add()
    {
        return view();
    }

    /**
     * 分类添加
     */
    public function cate_add_data()
    {
        $data = input('');

        try{
            validate(ShopCateValidate::class)->scene('add')->check($data);
        }catch(ValidateException $e){
            return Json::fail($e->getError());
        }

        $list = ShopCate::add($data);

        if($list === false) return Json::fail('添加失败');
        return Json::success('添加成功');
    }

    /**
     * 分类修改
     */
    public function cate_edit_data()
    {
        $data = input('');
        $datas = ['id' => $data['id'], $data['field'] => $data['value']];

        try{
            validate(ShopCateValidate::class)->scene('add')->check($datas);
        }catch(ValidateException $e){
            return Json::fail($e->getError());
        }

        $list = ShopCate::edit($datas);

        if($list === false) return Json::fail('修改失败');
        return Json::success('修改成功');
    }

    /**
     * 删除分类
     */
    public function cate_del()
    {
        $data = input('');

        try{
            validate(ShopCateValidate::class)->scene('show')->check($data);
        }catch(ValidateException $e){
            return Json::fail($e->getError());
        }

        $flag = ShopCate::destroy($data['id']);

        if($flag === false) return Json::fail('删除失败');
        return Json::success('删除成功');
    }
}
